<template>
  <div class="page-station">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="stationName"
            :label="$t('station.stationName') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.stationName"
              type="text"
              :placeholder="$t('station.stationNames')"
            />
          </el-form-item>
          <el-form-item
            prop="addr"
            :label="$t('station.addr') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.addr"
              type="text"
              :placeholder="$t('station.addr')"
            />
          </el-form-item>
          <el-form-item
            prop="account"
            :label="$t('station.account') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.account"
              type="text"
              :placeholder="$t('station.account')"
            />
          </el-form-item>
          <el-form-item
            prop="status"
            :label="$t('publics.status') + ':'"
            class="search-form-item"
          >
            <el-select
              v-model="searchForm.status"
              :placeholder="$t('publics.status')"
            >
              <el-option
                :label="$t('station.close')"
                :value="0"
              />
              <el-option
                :label="$t('station.business')"
                :value="1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('station.search') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch(searchFormRef)"
            >
              {{ $t('station.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t('crud.addTitle') }}
        </div>
      </div>
      <div class="table-con">
        <el-table
          ref="stationTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            fixed
            type="index"
            width="80"
            :label="$t('brand.serialNumber')"
          />
          <el-table-column
            align="left"
            prop="stationName"
            :label="$t('station.stationName')"
            width="270"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.stationName || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="addr"
            :label="$t('station.addr')"
            width="390"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.province }}{{ scope.row.city }}{{ scope.row.area
              }}{{ scope.row.addr }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="phone"
            :label="$t('station.number')"
            width="220"
          >
            <template #default="scope">
              <span class="table-cell-text">
                <span v-if="scope.row.phonePrefix && scope.row.phonePrefix">{{ scope.row.phonePrefix }}-</span>
                <span>{{ scope.row.phone }}</span>
              </span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="account"
            :label="$t('station.account')"
            width="210"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.account || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="status"
            :label="$t('publics.status')"
            min-width="150"
          >
            <template #default="scope">
              <span class="table-cell-text">
                <span v-if="scope.row.status === 1">{{ $t('station.business') }}</span>
                <span
                  v-else
                  type="danger"
                >{{ $t('station.close') }}</span>
              </span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="240"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.stationId)"
                >
                  {{ $t('crud.updateBtn') }}
                </div>
                <div
                  class="default-btn text-btn"
                  @click="onCheckStationAccount(scope.row.stationId)"
                >
                  {{ scope.row.account ? $t('admin.resetPwd') : $t('admin.createAcc') }}
                </div>
                <div
                  v-if="scope.row.status > 1"
                  class="default-btn text-btn"
                  @click="onAuditEventHandle(scope.row.stationId)"
                >
                  {{ scope.row.status === 3 ? $t('coupon.waitReview') : $t('coupon.applyBus') }}
                </div>
                <div
                  class="default-btn text-btn"
                  @click="onDelete(scope.row.stationId)"
                >
                  {{ $t('text.delBtn') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
      <!-- 新增编辑弹窗-->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdateRef"
        @refresh-data-list="refreshChange"
      />
      <!-- 账号弹窗-->
      <station-account
        v-if="stationAccountVisible"
        ref="stationAccountRef"
        @refresh-data-list="refreshChange"
      />
      <!-- 下线管理弹窗-->
      <offlineEventHandle
        v-if="offlineEventHandleVisible"
        ref="offlineEventRef"
        select-url="/admin/station/getOfflineHandleEventByStatonId"
        apply-url="/admin/station/auditApply"
        @refresh-data-list="refreshChange"
      />
    </div>
  </div>
</template>

<script setup>
import StationAccount from './components/account.vue'
import AddOrUpdate from './add-or-update.vue'
import { ElMessageBox, ElMessage } from 'element-plus'
const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
onMounted(() => {
  onGetDataList(page.value)
})
let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])

const searchForm = ref({
  stationName: '',
  addr: '',
  account: '',
  status: ''
}) // 搜索

const onGetDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/admin/station/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.value.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.value.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.value.total = data.total
  })
}
// 新增 / 修改
const addOrUpdateRef = ref(null)
const addOrUpdateVisible = ref(false)

const onAddOrUpdate = id => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}
const onDelete = id => {
  ElMessageBox.confirm($t('admin.isDeleOper'), $t('text.tips'), {
    confirmButtonText: $t('crud.filter.submitBtn'),
    cancelButtonText: $t('crud.filter.cancelBtn'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/admin/station/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          if (dataList.value.length === 1 && page.value.currentPage > 1) {
            page.value.currentPage = page.value.currentPage - 1
          }
          onGetDataList(page.value)
        }
      })
    })
  })
}
/**
 * 下线管理
 */
const offlineEventHandleVisible = ref(false)
const offlineEventRef = ref(null)
const onAuditEventHandle = id => {
  offlineEventHandleVisible.value = true
  nextTick(() => {
    offlineEventRef.value?.init(id)
  })
}
/**
 * 账号管理
 */
const stationAccountVisible = ref(false)
const stationAccountRef = ref(null)
const onCheckStationAccount = id => {
  stationAccountVisible.value = true
  nextTick(() => {
    stationAccountRef.value?.init(id)
  })
}

/**
 * 刷新回调
 */
const refreshChange = () => {
  addOrUpdateVisible.value = false
  onGetDataList(page.value)
}
const onSearch = (newData = false) => {
  page.value.currentPage = 1
  onGetDataList(page.value, newData)
}
/**
 * 重置表单
 * @param {String} formName 表单名称
 */
const searchFormRef = ref(null)

const onResetSearch = formName => {
  formName.resetFields()
}
const onPageSizeChange = val => {
  page.value.pageSize = val
  onGetDataList()
}
const onPageChange = val => {
  page.value.currentPage = val
  onGetDataList()
}
</script>
